﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Maticsoft.Common;

namespace Admin.WorkFlow.FormTable.PromiseSubContract
{
    public partial class PromiseSubContract_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLProject();
                BindDDLOrganize();
            }
        }
        /// <summary>
        /// 绑定项目信息
        /// </summary>
        private void BindDDLProject()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bllProject = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            DataSet ds = bllProject.GetAllList();
            if (ds != null &&ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ddlProjectName.DataSource = ds;
                ddlProjectName.DataTextField = "ProjectName";
                ddlProjectName.DataValueField = "ProjectInfoId";
                ddlProjectName.DataBind();
                ddlProjectName.Items.Insert(0, "");
            }
        }
        /// <summary>
        /// 绑定合作（分包）单位
        /// </summary>
        private void BindDDLOrganize()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Organize bllOrganzie = new ChinaSoft.BLL.BLL_tb_MidSoft_Organize();
            DataSet ds = bllOrganzie.GetAllList();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ddlOrganize.DataSource = ds;
                ddlOrganize.DataTextField = "OrganizeName";
                ddlOrganize.DataValueField = "OrganizeId";
                ddlOrganize.DataBind();
                ddlOrganize.Items.Insert(0,"");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (ddlProjectName.SelectedItem.Text=="")
            {
                strErr += "项目名称不能为空！\\n";
            }
            if (this.txtStage.Text.Trim().Length == 0)
            {
                strErr += "勘察阶段不能为空！\\n";
            }
            if (this.txtContractName.Text.Trim().Length == 0)
            {
                strErr += "联系人不能为空！\\n";
            }
            if (this.txtReason.Text.Trim().Length == 0)
            {
                strErr += "合作(分包)理由不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            int OrganizeId = int.Parse(this.ddlOrganize.SelectedValue);
            int PmFlowId = ChinaSoft.Common.Input.SetInt(Request.Params["PmFlowId"]);
            int ProjectInfoId =ChinaSoft.Common.Input.SetInt(this.ddlProjectName.SelectedValue);
            string Stage = this.txtStage.Text;
            string ContractName = this.txtContractName.Text;
            string Mobile = this.txtMobile.Text;
            string Reason = this.txtReason.Text;
            DateTime CreateTime =DateTime.Now;
            int CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);

            ChinaSoft.Model.Model_tb_MidSoft_PromiseSubContract model = new ChinaSoft.Model.Model_tb_MidSoft_PromiseSubContract();
            model.OrganizeId = OrganizeId;
            model.PmFlowId = PmFlowId;
            model.ProjectInfoId = ProjectInfoId;
            model.Stage = Stage;
            model.ContractName = ContractName;
            model.Mobile = Mobile;
            model.Reason = Reason;
            model.CreateTime = CreateTime;
            model.CreateUserId = CreateUserId;

            ChinaSoft.BLL.BLL_tb_MidSoft_PromiseSubContract bll = new ChinaSoft.BLL.BLL_tb_MidSoft_PromiseSubContract();
            bll.Add(model);
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }

        protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
        {
            int projectId = ChinaSoft.Common.Input.SetInt(ddlProjectName.SelectedValue);
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bllPorject=new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo  modelProject=new ChinaSoft.Model.Model_tb_MidSoft_ProjectInfo();
            modelProject=bllPorject.GetModel(projectId);
            if (modelProject != null)
            {
                lblProjectNumber.Text = modelProject.ProjectNumber;
            }
        }
    }
}